<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="规则类型">
          <j-dict-select-tag v-decorator="['fdRuletype', validatorRules.fdRuletype ]"placeholder="请输入规则类型" dictCode="dict_param,FDRULENAME,text" :triggerChange="true" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="安全等级">
          <j-dict-select-tag v-decorator="['fdRulelevel', validatorRules.fdRulelevel ]"placeholder="请输入安全等级" dictCode="dict_param,RULELEVE,text" :triggerChange="true" />
        </a-form-item>
       <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="规则名称">
          <a-input placeholder="请输入规则名称" v-decorator="['fdRulename', validatorRules.fdRulename ]" />
        </a-form-item>-->
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="规则算法">
          <a-textarea :rows="3" placeholder="请输入规则算法" v-decorator="['fdAlgorithm', validatorRules.fdRulename ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="规则描述">
          <a-textarea :rows="3" placeholder="规则描述" v-decorator="['fdRuledesc', {}]" />
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol">
          <a-input placeholder="请输入数据"  v-model="algorithm"  style="left: 156px;width: 280px"/>
          <a-button type="primary" @click="TestDesensitization" style="left: 165px;">测试脱敏</a-button>
        </a-form-item>
        <div style="padding-left: 90px">
            {{this.con}}
        </div>

       <!-- <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="函数支持数据库">
          <a-input placeholder="请输入函数支持数据库" v-decorator="['fdSupportdb', validatorRules.fdSupportdb ]" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="顺序">
          <a-input-number v-decorator="[ 'fdOrder', validatorRules.fdOrder ]" />
        </a-form-item>
        -->
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { httpAction,getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import ATextarea from 'ant-design-vue/es/input/TextArea'

  export default {
    name: "RuleModal",
    components: { ATextarea },
    data () {

      return {
        title:"操作",
        visible: false,
        model: {},
        algorithm:"",
        con:"",
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },

        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
        fdRulename:{rules: [{ required: true, message: '请输入规则名称!' }]},
        fdRuledesc:{rules: [{ required: true, message: '请输入规则描述!' }]},
        fdAlgorithm:{rules: [{ required: true, message: '请输入规则算法!' }]},
          fdRulelevel:{rules: [{ required: true, message: '请输入规则等级!' }]},
        fdRuletype:{rules: [{ required: true, message: '请输入算法类型!' }]},
        fdSupportdb:{rules: [{ required: true, message: '请输入函数支持数据库!' }]},
        fdOrder:{rules: [{ required: true, message: '请输入顺序!' }]},
        },
        url: {
          add: "/rule/rule/add",
          edit: "/rule/rule/edit",
          testDesensitization:"/rule/rule/test",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'fdRulename','fdRuledesc','fdAlgorithm','fdRulelevel','fdRuletype','fdSupportdb','fdOrder','createUser','updateUser','isDelete'))
		  //时间格式化
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化

            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })



          }
        })
      },
      handleCancel () {
        this.algorithm="",
          this.con="",
        this.close()
      },
      TestDesensitization(){
        var params = {algorithmText:this.algorithm,fdAlgorithm:fdAlgorithm._value};
        getAction(this.url.testDesensitization, params).then(res => {
          if(res.success){
            this.con = res.result;
          } else {
            this.con = res.message;
          }
        }).finally(() => {
        /* this. loading = false*/
        })
      },
    }
  }
</script>

<style lang="less" scoped>

</style>